﻿using System;
using Microsoft.SharePoint;

namespace WoltersKluwer.SPChainGang.Core.ExtensionMethods
{
    public static class ListItemExtensions
    {
        static SPField GetField(this SPListItem item, string fieldInternalName)
        {
            return item.Fields.GetFieldByInternalName(fieldInternalName);
        }

        public static object GetFieldValue(this SPListItem item, string fieldInternalName)
        {
            return item[item.Fields.GetFieldByInternalName(fieldInternalName).Id];
        }

        public static string GetString(this SPListItem item, string fieldInternalName)
        {
            var objectValue = item.GetFieldValue(fieldInternalName);
            var stringValue = objectValue != null ? objectValue.ToString() : string.Empty;
            return stringValue;
        }

        public static bool GetBoolean(this SPListItem item, string fieldInternalName)
        {
            bool result;
            return bool.TryParse(item.GetString(fieldInternalName), out result) && result;
        }

        public static bool GetBoolean(this SPListItem item, string fieldInternalName, bool defaultValue)
        {
            bool result;
            return bool.TryParse(item.GetString(fieldInternalName), out result) ? result : defaultValue;
        }

        public static SPFieldUserValue GetUserLookupValue(this SPListItem item, string fieldName)
        {
            var stringFieldValue = item.GetString(fieldName);
            var userField = (SPFieldUser)item.GetField(fieldName);

            return userField != null && !string.IsNullOrEmpty(stringFieldValue)
                       ? (SPFieldUserValue)userField.GetFieldValue(stringFieldValue)
                       : null;
        }

        public static SPFieldLookupValue GetLookupValue(this SPListItem item, string fieldInternalName)
        {
            String stringFieldValue = String.Empty;
            try
            {
                stringFieldValue = item.GetString(fieldInternalName);
            }
            catch (ArgumentException)
            {
            }
            return !string.IsNullOrEmpty(stringFieldValue)
                       ? new SPFieldLookupValue(stringFieldValue) : null;
        }

        public static string GetLookupValueString(this SPListItem item, string fieldInternalName)
        {
            var fieldValue = item.GetLookupValue(fieldInternalName);
            return fieldValue != null ? fieldValue.LookupValue : string.Empty;
        }

        public static void SetProperty(this SPListItem item, String fieldName, object value)
        {
            if (item.Fields.ContainsField(fieldName))
                item[fieldName] = value;
        }

        public static SPUser GetUserValue(this SPListItem item, String fieldName)
        {
            var fieldValue = item.GetString(fieldName);
            return !String.IsNullOrEmpty(fieldValue) ? new SPFieldUserValue(item.Web, fieldValue).User : null;
        }

        public static DateTime? GetNullableDateTime(this SPListItem item, String fieldName)
        {
            var value = item.GetFieldValue(fieldName);
            return value != null ? Convert.ToDateTime(value) : (DateTime?)null;
        }

        public static int? GetNullableInt(this SPListItem item, String fieldName)
        {
            return item[fieldName] != null ? Convert.ToInt32(item[fieldName]) : (int?) item[fieldName];
        }

        public static float? GetNullableFloat(this SPListItem item, String fieldName)
        {
            return item[fieldName] != null ? float.Parse(item[fieldName].ToString()) : (float?)item[fieldName];
        }

        public static SPFieldUrlValue GetUrl(this SPListItem item, String fieldName)
        {
            var fieldValue = item.GetString(fieldName);
            return !String.IsNullOrEmpty(fieldValue) ? new SPFieldUrlValue(fieldValue) : null;
        }
    }
}